import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 静态路由表
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'route.dataOverview', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// 动态路由表
export const asyncRoutes = [
  // 后台管理
  {
    path: '/backstage',
    component: Layout,
    redirect: '/backstage/users',
    name: 'backstage',
    meta: { title: 'route.backstage', icon: 'people' },
    children: [
      {
        path: 'users',
        name: 'users',
        component: () => import('@/views/backstage/users/index.vue'),
        meta: { title: 'route.users', icon: 'component' }
      },
      {
        path: 'menus',
        name: 'menus',
        component: () => import('@/views/backstage/menus/index.vue'),
        meta: { title: 'route.menus', icon: 'component' }
      },
      {
        path: 'permissions',
        name: 'permissions',
        component: () => import('@/views/backstage/permissions/index.vue'),
        meta: { title: 'route.permissions', icon: 'component' }
      },
      {
        path: 'logs',
        name: 'logs',
        component: () => import('@/views/backstage/logs/index.vue'),
        meta: { title: 'route.logs', icon: 'component' }
      }
    ]
  },
  // 企业管理
  {
    path: '/enterprise',
    component: Layout,
    children: [{
      path: 'enterprise',
      name: 'enterprise',
      component: () => import('@/views/enterprise/index'),
      meta: { title: 'route.enterprise', icon: 'peoples' }
    }]
  },
  // 题库管理
  {
    path: '/questionbank',
    component: Layout,
    redirect: '/questionbank/baseqb',
    name: 'questionbank',
    meta: { title: 'route.questionbank', icon: 'documentation' },
    children: [
      {
        path: 'baseqb',
        name: 'baseqb',
        component: () => import('@/views/questionbank/BaseQuestionBank/index.vue'),
        meta: { title: 'route.baseqb', icon: 'component' }
      },
      {
        path: 'selectedqb',
        name: 'selectedqb',
        component: () => import('@/views/questionbank/SelectedQuestionBank/index.vue'),
        meta: { title: 'route.selectedqb', icon: 'component' }
      },
      {
        path: 'testentry',
        name: 'testentry',
        component: () => import('@/views/questionbank/TestEntry/index.vue'),
        meta: { title: 'route.testentry', icon: 'component' }
      },
      {
        path: 'groupql',
        name: 'groupql',
        component: () => import('@/views/questionbank/GroupQuestionList/index.vue'),
        meta: { title: 'route.groupql', icon: 'component' }
      }
    ]
  },
  // 学科管理
  {
    path: '/subjectmanage',
    component: Layout,
    redirect: '/subjectmanage/subject',
    name: 'subjectmanage',
    meta: { title: 'route.subjectmanage', icon: 'table' },
    children: [
      {
        path: 'subject',
        name: 'subject',
        component: () => import('@/views/subjectmanage/subject/index.vue'),
        meta: { title: 'route.subject', icon: 'component' }
      },
      {
        path: 'catalogue',
        name: 'catalogue',
        component: () => import('@/views/subjectmanage/catalogue/index.vue'),
        meta: { title: 'route.catalogue', icon: 'component' }
      },
      {
        path: 'label',
        name: 'label',
        component: () => import('@/views/subjectmanage/label/index.vue'),
        meta: { title: 'route.label', icon: 'component' }
      }
    ]
  },
  // 面试技巧
  {
    path: '/interview',
    component: Layout,
    children: [{
      path: 'interview',
      name: 'interview',
      component: () => import('@/views/interview/index'),
      meta: { title: 'route.interview', icon: 'component' }
    }]
  },
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
